#ifndef MAP_H
#define MAP_H

#include <QtGui>
class MapChunk;

struct map_cal_point
{
    int x;
    int y;
    double lat;
    double lon;
};

// map_id _title image_size chunk_size caldata
class Map : public QRectF
{
    friend class MapChunk;

public:

    static bool loadCalibration(const QString&, QList<map_cal_point>&);

    static bool loadOziMap( const QString&, QList<map_cal_point>&);
    static bool loadCompeImp( const QString&, QList<map_cal_point>&);
    static bool loadFreeTrackCal( const QString&, QList<map_cal_point>&);

public:
    Map(const QString&);
    Map(QTextStream&);

    ~Map();

    QString title() const;
    void setTitle(const QString&);

    bool isValidMap() const;
    bool isCalibrated() const;
    bool isEnabledMap() const;
    void enableMap(bool);

    void paint(QPainter&, const QTransform&, const QRectF&, double, double ) const;
    void paint(QPainter&, const QRect&, const QRect&) const;

    QString serializeMap();
    void clearCache();

    void calibrationPoints( QList<map_cal_point>& ) const;
    void setCalibrationPoints( const QList<map_cal_point>& );

    QSize pixelSize() const;
    int maxZoom() const;


private:
    static QStringList used_ids;

private:
    void initialize();
    bool setMapId(const QString&);

    bool validMap;
    bool calibrated;
    bool _mapEnabled;

    QString _title;
    QString map_id;
    QSize image_size;
    int chunk_size;


private:
    QList<map_cal_point> cal_points;

private:
    void calibrateMap();
    int bestZoomFactor(int) const;
    void calculateStaticMeasures();
    int max_zoom;
    QHash<int, int> max_cols;
    QHash<int, int> max_rows;

    QTransform wgs84_to_pixel;
    QTransform pixel_to_wgs84;

};

#endif // MAP_H
